package com.competition.web.admin.controller;

import java.io.IOException;
import java.util.function.Function;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.competition.web.admin.config.UploadTypeConf;
import com.competition.web.admin.service.IUploadService;
import com.competition.web.admin.util.SpringBeanUtil;
import com.response.vo.ResponseModel;
import com.returns.value.ICodeAndData;

@RestController
@RequestMapping("upload")
public class UploadController {

	private IUploadService uploadService;

	private UploadTypeConf uploadTypeConf;
	
	private DataSourceTransactionManager dataSourceTransactionManager;

	
	@Autowired
	public void setDataSourceTransactionManager(DataSourceTransactionManager dataSourceTransactionManager) {
		this.dataSourceTransactionManager = dataSourceTransactionManager;
	}


	public UploadController(UploadTypeConf typeConf) {
		this.uploadTypeConf = typeConf;
	}
	
	private String getToken(HttpServletRequest httpServletRequest) {
		return httpServletRequest.getHeader("Authorization");
	}

	@PostMapping("uploadXlsx")
	public ResponseModel<String> uploadXlxs(MultipartFile multipartFile, HttpServletRequest httpServletRequest) throws IOException {

//		InputStream inputStream = multipartFile.getInputStream();
//		
//		this.getIUploadService(httpServletRequest);
//		
//		ICodeAndData<String> codeAndData = this.uploadService.parseXlxs(inputStream, multipartFile.getOriginalFilename(), multipartFile.getSize());
//
//		if (0 != ResponseCode.SUCCESS.compareTo(codeAndData.getResponseCode())) {
//			return ResponseModel.<String>errorMsg("", codeAndData.getResponseCode());
//		}
//		
//		int userId = Integer.valueOf(httpServletRequest.getAttribute("userId").toString());
//		
//		EasyExcel.read(
//			new File(codeAndData.getData()), 
//			VisitCustomerExcel.class, 
//			new XlxsReadListener(
//					SpringBeanUtil.getBean(IVisitCustomerService.class),
//					SpringBeanUtil.getBean(IStaffService.class),
//					userId,
//					this.getToken(httpServletRequest)
//			)
//		).registerConverter(new EasyExcelLocalTimeConverter())
//		.sheet().doRead();
//		
		return ResponseModel.<String>success("");

	}
	
	
	protected void getIUploadService(HttpServletRequest httpServletRequest) {
		
		int type = Integer.valueOf(httpServletRequest.getHeader("type"));

		uploadService = SpringBeanUtil.getBean(this.uploadTypeConf.getType()[type]);
		
	}
	
	/**
	 * 
	 * @param multipartFile
	 * @param httpServletRequest
	 * @return
	 * @throws IOException
	 */
	private <V> ResponseModel<String> upload(MultipartFile multipartFile, HttpServletRequest httpServletRequest,
			Function<V, String> function) {

		try {

			this.getIUploadService(httpServletRequest);

			ICodeAndData<String> codeAndData = uploadService.uploadFileToOSS(multipartFile.getInputStream(),
					multipartFile.getOriginalFilename(), function, multipartFile.getSize());

			return ResponseModel.<String>response(codeAndData);
		} catch (Exception e) {

			throw new Error(e);
		}
	}

	
	

}
